#! /bin/bash

# Ameir Abdeldayem
# http://www.ameir.net
# You are free to modify and distribute this code,
# so long as you keep my name and URL in it.
# Last modified: August 1, 2006
#-------------------------------------------------------------

# your server's name
SERVER=ameir.net

# directory to backup to
BACKDIR=~/backups

# date format that is appended to filename
DATE=`date +'%m-%d-%Y'`

# directories to backup, separated by a space
# if this script is a level above a directory you want to backup,
# you can simply enter its name, otherwise, type the absolute path, 
# with or without a trailing slash.
DIRS="/home/user/public_html /home/user/directory2"

# set to 'y' if you want to backup all your folders. this will backup
# all files and folders in the script's parent directory
BACKALL=n

#----------------------Mail Settings--------------------#

# set to 'y' if you'd like to be emailed the backup (requires mutt)
MAIL=n

# email addresses to send backups to, separated by a space
EMAILS="user@gmail.com user@inbox.com user@goowy.com"

# email subject
SUBJECT="Directory Backup on $SERVER ($DATE)"

#----------------------FTP Settings--------------------#

# set "FTP=y" if you want to enable FTP backups
FTP=y

# FTP server settings; should be self-explanatory
FTPHOST="ftp.server.com"
FTPUSER="user"
FTPPASS="password"

# directory to backup to. if it doesn't exist, file will be uploaded to 
# first logged-in directory
FTPDIR="backups"

#-------------------Deletion Settings-------------------#

# delete old files?
DELETE=y

# how many days of backups do you want to keep?
DAYS=3

#----------------------End of Settings------------------#
# check of the backup directory exists
# if not, create it
if  [ -e $BACKDIR ]
then
 echo Backups directory already exists
else
mkdir $BACKDIR
fi


# This is a list of folders to be backed up.
# You can add more entries if you want more
# directories to be backed up. The ${PWD##*/}
# from the first entry gets the base name from
# the current directory and uses it in the filename.
# Format: zip -9 -r (where to save) (what to backup).
# Be sure to include $BACKDIR/ in the beginning
# so that the file is saved in the backup directory.

if  [ $BACKALL = "y" ]
then
echo Backing up entire parent directory...
zip -9 -r $BACKDIR/${PWD##*/}-backup-$SERVER-$DATE.zip ./
else
echo Backing up selected directories...
for directory in $DIRS
do
DIR=`echo $directory |  sed -e 's/^\///g'  -e 's/\/$//g' \
-e 's/~//g' -e 's/\.//g' -e 's/home\///g' -e 's/\//_/g'`
echo Backing up folder named $directory as $DIR...
zip -9 -r $BACKDIR/$DIR-backup-$SERVER-$DATE.zip $directory
done
fi

if  [ $MAIL = "y" ]
then
BODY="Your backup is ready! Find more useful scripts and info at http://www.ameir.net"
ATTACH=`for file in $BACKDIR/*$DATE.zip; do echo -n "-a ${file} "; done`

echo $BODY | mutt -s "$SUBJECT" $ATTACH $EMAILS
        
echo "Your backup has been emailed to you!"
fi

if  [ $FTP = "y" ]
then
cd $BACKDIR
ATTACH=`for file in *$DATE.zip; do echo -n -e "put ${file}\n"; done`

ftp -nv <<EOF
open $FTPHOST
user $FTPUSER $FTPPASS
cd $FTPDIR
$ATTACH
quit
EOF
fi

if  [ $DELETE = "y" ]
then
find $BACKDIR -name "*.zip" -mtime $DAYS -exec rm {} \;

if  [ $DAYS = "1" ]
then
echo "Yesterday's backup has been deleted"
else
echo "The backup from $DAYS days ago has been deleted"
fi
fi

echo Your backup is complete!
